package org.application.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.application.Entity.Piece;
import org.application.enums.PieceSideEnum;
import org.application.service.MoveValidator;
import java.util.Map;

@Slf4j
public class KingMoveValidator implements MoveValidator {
    @Override
    public boolean validateMove(Map<String, Piece> pieceMap, Piece selectedPiece, int startRow, int startCol, int targetRow, int targetCol) {
        // 检查目标位置是否在九宫格范围内
        if (!isWithinPalace(selectedPiece, targetRow, targetCol)) {
            log.warn("帅的目标位置不在九宫格范围内");
            return false;
        }

        // 检查是否仅移动一格
        int rowDiff = Math.abs(targetRow - startRow);
        int colDiff = Math.abs(targetCol - startCol);
        if ((rowDiff == 1 && colDiff == 0) || (rowDiff == 0 && colDiff == 1)) {
            return true;
        }

        log.warn("帅只能上下左右移动一格");
        return false;
    }

    private boolean isWithinPalace(Piece selectedPiece, int row, int col) {
        // 判断九宫格范围，依据棋子颜色区分
        if (selectedPiece.getSide() == PieceSideEnum.RED) {
            return row >= 0 && row <= 2 && col >= 3 && col <= 5; // 黑方九宫格范围
        } else if (selectedPiece.getSide() == PieceSideEnum.BLACK) {
            return row >= 7 && row <= 9 && col >= 3 && col <= 5; // 红方九宫格范围
        }
        return false;
    }
}
